package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.CourseCatalogAdapter;
import com.yaozheng.vocationaltraining.adapter.MemoryWealthAdapter;
import com.yaozheng.vocationaltraining.db.CoursewareCacheDao;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.CourseCatalogService;
import com.yaozheng.vocationaltraining.service.impl.CourseCatalogServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 课程目录
 *
 * @author Administrator
 */
@EActivity(R.layout.activity_news_list)
public class CourseCatalogActivity extends BaseActivity implements
        ITokenBaseView, SwipeRefreshLayout.OnRefreshListener, ILoadMoreView {
    @ViewById
    ListView listView;
    @Extra
    String title;

    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;
    @Bean(CourseCatalogServiceImpl.class)
    CourseCatalogService courseCatalogService;

    CourseCatalogAdapter courseCatalogAdapter;

    int lastId;
    ListViewLoadMoreManage listViewLoadMoreManage;

    CoursewareCacheDao coursewareCacheDao;
    @Extra
    int menuType;

    JSONArray coursesDataList;

    @AfterViews
    public void initView() {
        setHeadTitle(title);
        courseCatalogService.init(this);
        coursewareCacheDao = new CoursewareCacheDao(this);
        onRefresh();
//        swipeRefreshLayout.postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                onRefresh();
//
//            }
//        }, 500);
        swipeRefreshLayout.setOnRefreshListener(this);
        listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
                listView);
    }

    /**
     * 加载数据
     * <p/>
     * 最后一个id
     */
    public void loadData() {
        if (menuType == 4) {
            cancelLoadDataProgressDialog();
            coursesDataList = coursewareCacheDao.getList();
            refreshData();
        } else {
            courseCatalogService.list(menuType, getIntentInt("subCatId"),
                    lastId);
        }
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        coursesDataList = TypeUtils.getJsonArray(jsonObject, "data", "courses");
        refreshData();

    }

    public void refreshData() {
        if (coursesDataList != null) {
            if (courseCatalogAdapter == null) {
                courseCatalogAdapter = new CourseCatalogAdapter(this,
                        coursesDataList);
                listView.setAdapter(courseCatalogAdapter);

            } else {
                if (lastId == 0) {
                    courseCatalogAdapter.getDataList().clear();
                }
                courseCatalogAdapter.getDataList().addAll(coursesDataList);
                courseCatalogAdapter.notifyDataSetChanged();
            }
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    @ItemClick(R.id.listView)
    public void listViewItemClick(int position) {
        if (courseCatalogAdapter != null) {
            JSONObject courseCatalogJsonObject = TypeUtils.getJsonObject(
                    courseCatalogAdapter.getDataList(), position);
            Intent intent = new Intent();
            intent.putExtra("title", TypeUtils.getJsonString(courseCatalogJsonObject, "subject"));
            intent.putExtra("courserJsonStr",
                    courseCatalogJsonObject.toString());
            intent.putExtra("isLoadNew", true);
            intent.putExtra("subCatId", getIntentInt("subCatId"));
            if ("video".equals(TypeUtils.getJsonString(courseCatalogJsonObject,
                    "format"))) {

                intent.setClassName(this,
                        Constants.VIDEO_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);

            } else if ("image".equals(TypeUtils.getJsonString(
                    courseCatalogJsonObject, "format"))) {
                intent.setClassName(
                        this,
                        Constants.IMAGE_LIST_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);
            } else
                return;
            startActivityForResult(intent, Constants.REQUEST_CODE);
        }
    }

    @Override
    public boolean isResponseResult() {
        return !isFinishing();
    }

    @Override
    public void loadMoreData() {
        if (courseCatalogAdapter != null) {
            int lastId = courseCatalogAdapter.getLastId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }

    /**
     * 结果
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE) {
            if (resultCode == Constants.RESULT_OK_REFRESH_DATA) {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
